package com.ap.core.domain;

import org.apache.commons.lang.StringUtils;

import javax.persistence.Embeddable;
import java.io.Serializable;

/**
 * Specific type representing an sort code.
 */
@Embeddable
public final class SortCode implements Serializable{

    private static final int SORT_CODE_LENGTH = 6;

    /** Integer value of the sort code *. */
    private String sortCode;

    /**
     * private constructor which creates an <code>SortCode</code> using a supplied string representation of a sort code.
     * 
     * @param sortCode the sort code
     */
    private SortCode(String sortCode) {
        if (StringUtils.isNotBlank(sortCode)) {
            this.sortCode = StringUtils.leftPad(sortCode, SORT_CODE_LENGTH, '0');
        }
    }

    /**
     * Protected no arg for JPA.
     */
    protected SortCode() {
    }

    /**
     * Method to return a sort code 
     * 
     * @param sortCode the sort code
     * 
     * @return SortCode
     */
    public static SortCode valueOf(String sortCode) {

        return new SortCode(sortCode);
    }

    
    @Override
    public int hashCode() {
        return sortCode.hashCode();
    }

    /**
     * 
     * {@inheritDoc}
     * 
     */
    @Override
    public boolean equals(Object o) {
        if (this == o){
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }    
        SortCode comparison = (SortCode) o;
        return sortCode.equals(comparison.sortCode);
    }

    @Override
    public String toString() {
        return sortCode;
    }

    public Integer getIntegerValue() {
    	return Integer.valueOf(sortCode);
    }
}
